<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Image;
use App\Services\OSS;

class ArticleController extends Controller
{
    //文件上传
    public function upload(Request $request){
        $photo = $request -> file('photo');
        //判断文件是否上传成功
        if(!$photo -> isValid()){
            return response()->json(['ServerNo'=>400,"ResultData"=>"无效的上传文件"]);
        }
        //获取源文件的扩展名
        $ext = $photo->getClientOriginalExtension();

        //新文件名
        $newfile = md5(time().rand(1000,9999)).'.'.$ext;

        //文件上传的指定路径
//        $path = public_path('uploads');

        //将源文件从临时目录移动到指定目录
//        if(!$photo->move($path,$newfile)){
//            return response()->json(['ServerNo'=>400,"ResultData"=>"文件保存失败"]);
//        }

        //使用Image类修改图片后再保存文件到本地
//        $res = Image::make($photo) -> resize(100,100) -> save($path.'/'.$newfile);

        //将文件上传到阿里oss指定仓库    第一个参数：文件上传到oss后的新文件名 第二个参数:要上传的文件资源
        $res = OSS::upload($newfile,$photo->getRealPath());

        if($res){
            return response()->json(['ServerNo'=>200,"ResultData"=>$newfile]);
        }else{
            return response()->json(['ServerNo'=>400,"ResultData"=>'图片上传你失败']);
        }



    }

    public function index()
    {
//        $permission = Cate::sortCate();
        $permission = (new Cate)->sortCate();
        return view('admin.article.list',compact('permission'));
    }

    public function create()
    {
        $cate = (new Cate)->sortCate();
        return view('admin.article.add',compact('cate'));
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $res = Cate::create($input);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    public function edit($id)
    {
        $cate = Cate::find($id);
        $pcate = Cate::where('pid',0)->get();
        return view('admin.cate.edit',compact('cate','pcate'));
    }

    public function update(Request $request, $id)
    {
        $user = $request -> all();
        $res = Cate::where("id",$id)->update($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    public function destroy($id)
    {
        $user = Cate::find($id);
        $res = $user->delete();
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    public function delAll(Request $request){
        $res = Cate::destroy($request->input('ids'));
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }
}
